function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
import React, { useLayoutEffect, useRef } from 'react';
import { iup } from "../util";
import { Fragment as _Fragment } from "react/jsx-runtime";
import { jsxs as _jsxs } from "react/jsx-runtime";
var globalWindow = window;

/**
 * 组件自适应容器
 * @param children 需要自适应的组件
 * @param style
 * @param onResize 父容器高宽变化时回调，undefined 则直接返回children组件
 * @param delay
 * @constructor
 */
export function AutoResize(_ref) {
  var children = _ref.children,
    style = _ref.style,
    onResize = _ref.onResize,
    beforeResize = _ref.beforeResize,
    _ref$delay = _ref.delay,
    delay = _ref$delay === void 0 ? undefined : _ref$delay;
  var objRef = useRef();
  var mountRef = useRef(false);
  var beforeResizeRef = useRef(beforeResize);
  beforeResizeRef.current = beforeResize;
  var supportResizeObserver = globalWindow.ResizeObserver;
  useLayoutEffect(function () {
    mountRef.current = true;
    if (onResize) {
      var lastBcr = {
        width: 0,
        height: 0
      };
      var pEl = objRef.current.parentElement;
      var offset = pEl ? {
        x: (parseFloat(iup.getStyle(pEl, 'paddingLeft')) || 0) + (parseFloat(iup.getStyle(pEl, 'paddingRight')) || 0),
        y: (parseFloat(iup.getStyle(pEl, 'paddingTop')) || 0) + (parseFloat(iup.getStyle(pEl, 'paddingBottom')) || 0)
      } : {
        x: 0,
        y: 0
      };
      var onResizeEvent = function onResizeEvent() {
        var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
        if (!mountRef.current || !objRef.current || !objRef.current.offsetParent) {
          return;
        }
        var bcr = {
          width: objRef.current.clientWidth,
          height: objRef.current.clientHeight
        };
        if (bcr.width !== lastBcr.width || bcr.height !== lastBcr.height) {
          lastBcr.width = bcr.width;
          lastBcr.height = bcr.height;
          onResize({
            width: bcr.width - offset.x,
            height: bcr.height - offset.y
          }, options);
        }
      };
      var debounce = function debounce(func, delayTime) {
        var timer = null;
        var exec = delayTime ? setTimeout : window['requestIdleCallback'] || window['requestAnimationFrame'];
        var cancel = delayTime ? clearTimeout : window['cancelIdleCallback'] || window['cancelAnimationFrame'];
        return function () {
          timer && cancel(timer);
          !timer && func({
            flushSync: true
          });
          timer = exec(function () {
            timer = null;
            func();
          }, delayTime || undefined);
        };
      };
      var resizeObserver;
      var targetView;
      var debounceFn = debounce(onResizeEvent, delay !== null && delay !== void 0 ? delay : 60);
      var resize = function resize() {
        var _beforeResizeRef$curr;
        if (!mountRef.current || !objRef.current) {
          return;
        }
        (_beforeResizeRef$curr = beforeResizeRef.current) === null || _beforeResizeRef$curr === void 0 || _beforeResizeRef$curr.call(beforeResizeRef, {
          width: objRef.current.clientWidth,
          height: objRef.current.clientHeight
        });
        debounceFn();
      };
      if (supportResizeObserver) {
        resizeObserver = new supportResizeObserver(resize);
        resizeObserver.observe(objRef.current);
      } else {
        targetView = objRef.current.contentWindow;
        if (targetView) {
          targetView.addEventListener('resize', resize);
        } else {
          setTimeout(function () {
            var _objRef$current;
            (_objRef$current = objRef.current) === null || _objRef$current === void 0 || (_objRef$current = _objRef$current.contentWindow) === null || _objRef$current === void 0 || _objRef$current.addEventListener('resize', resize);
          });
        }
      }
      if (objRef.current.clientWidth > 0) {
        onResizeEvent();
      } else if (!supportResizeObserver) {
        // 不会自动触发resize，导致数据渲染不出来
        setTimeout(function () {
          objRef.current.clientWidth > 0 && onResizeEvent();
        });
      }
      return function () {
        mountRef.current = false;
        if (resizeObserver) {
          resizeObserver.disconnect();
          resizeObserver = null;
        } else {
          var _objRef$current2;
          (_objRef$current2 = objRef.current) === null || _objRef$current2 === void 0 || (_objRef$current2 = _objRef$current2.contentWindow) === null || _objRef$current2 === void 0 || _objRef$current2.removeEventListener('resize', resize);
        }
      };
    }
    return function () {
      mountRef.current = false;
    };
  }, [onResize]);
  var props = supportResizeObserver ? {} : {
    type: 'text/html',
    data: 'about:blank'
  };
  return /*#__PURE__*/_jsxs(_Fragment, {
    children: [children, onResize && /*#__PURE__*/React.createElement(supportResizeObserver ? 'div' : 'iframe', _objectSpread({
      ref: objRef,
      tabIndex: -1,
      style: _objectSpread({
        display: 'block',
        position: 'absolute',
        top: 0,
        left: 0,
        height: '100%',
        width: '100%',
        overflow: 'hidden',
        opacity: 0,
        pointerEvents: 'none',
        zIndex: -1
      }, style)
    }, props))]
  });
}

/**
 * 不支持ResizeObserver的浏览器，resize事件绑定在window页面，需要手动调用触发
 */
export function polyfillResize() {
  var supportResizeObserver = globalWindow.ResizeObserver;
  if (!supportResizeObserver) {
    window.dispatchEvent(new Event('resize'));
  }
}